<?php
include_once("../../includes/config.inc.php");
check_login();
$suserSession=get_suser();
$customer_id=$suserSession['id'];
$model=D('info_common');
$smarty->assign('cateName','多图文回复');

//根据id获取子图文的信息
if($_REQUEST['a']=='getSubItem'){
    $res=D('info_common_detail')->find((int)$_REQUEST['id']);
    echo json_encode($res);
    die;
}

//删除子图文
if($_REQUEST['a']=='delDetail'){
    D('info_common_detail')->del((int)$_REQUEST['id']);
    die('suc');
}


//敏感词
$sense=array();
$senseArr = D('wx_sense')->dataArr();
foreach($senseArr as $k=>$v){
    if($v['word']) $sense[]=$v['word'];
}
$senseRow = $sense ? json_encode($sense) : '';
$smarty->assign('senseRow',$senseRow);

/**
 * 第一次添加图文
 */
if($_REQUEST['a']=='add'){
	if(method_is('post')){
		$data=$_POST;
        //关键词过滤
        $data['keyword'] = str_replace("，",",",$data['keyword']);
        $keyArray = explode(',',$data['keyword']);
        $keyTable = D('keyword_list');
        foreach($keyArray as $v){
            $kRes=$keyTable->where("customer_id='{$customer_id}' and keyword='{$v}'")->dataArr();
            if($kRes && !$data['id']){
                die("关键词 ".$v." 已存在，请更换");
            }elseif($data['id']){
                $kEditRes=$keyTable->where("info_id!='".$data['id']."' and customer_id='{$customer_id}' and keyword='{$v}'")->dataArr();
                if($kEditRes){
                    die("关键词 ".$v." 已存在，请更换");
                }
            }
        }
        
        if(!$data['pic_showincontent']) $data['pic_showincontent']='0';
        $res=$model->insert(array(
            'customer_id'=>$customer_id,'info_type'=>'3',
            'keyword'=>$data['keyword'],'state'=>$data['state']
        ));
        //保存图文详情
        Model_Table::get('InfoCommonDetail')->insert(array(
            'info_common_id'=>$res,'title'=>$data['title'],'pic'=>$data['pic'],
            'msg_content'=>$data['msg_content'],'url'=>$data['url'],
            'pic_showincontent'=>$data['pic_showincontent'],'order_num'=>$data['order_num']
        ));
        //保存keyword_list表
        $info_id = $data['id'] ? $data['id'] : $res;
        $arr=array('customer_id'=>$customer_id,'info_id'=>$info_id,'keyword'=>$data['keyword'],'info_type'=>'multi');
        Model_Table::get('KeywordList')->saveForKeywords($arr);
        
        die('suc'.$res);
	}else{
		$smarty->setTpl('info/templates/multi_add.html')->display();die();
	}
}

/**
 * 图文修改
 * 一条信息对应info_commmon中一条数据
 * 一条信息可能对应keyword_list中多条数据
 */
if($_REQUEST['a']=='edit'){
	if(method_is('post')){
		$data=$_POST;
        if(!$data['pic_showincontent']) $data['pic_showincontent']='0';
        //第一次保存
        if($data['info_type_form']=='one'){//第一条图文的修改
            //关键词过滤
            $data['keyword'] = str_replace("，",",",$data['keyword']);
            $keyArray = explode(',',$data['keyword']);
            $keyTable = D('keyword_list');
            foreach($keyArray as $v){
                $kRes=$keyTable->where("customer_id='{$customer_id}' and keyword='{$v}'")->dataArr();
                if($kRes && !$data['id']){
                    die("关键词 ".$v." 已存在，请更换");
                }elseif($data['id']){
                    $kEditRes=$keyTable->where("info_id!='".$data['id']."' and customer_id='{$customer_id}' and keyword='{$v}'")->dataArr();
                    if($kEditRes){
                        die("关键词 ".$v." 已存在，请更换");
                    }
                }
            }
            
            $model->updateById($data['id'],array(
                'keyword'=>$data['keyword'],'state'=>$data['state']
            ));
            Model_Table::get('InfoCommonDetail')->updateById($data['child_id'],array(
                'title'=>$data['title'],'pic'=>$data['pic'],
                'msg_content'=>$data['msg_content'],'url'=>$data['url'],
                'pic_showincontent'=>$data['pic_showincontent'],'order_num'=>$data['order_num']
            ));
            //保存keyword_list表
            $info_id = $data['id'] ? $data['id'] : $res;
            $arr=array('customer_id'=>$customer_id,'info_id'=>$info_id,'keyword'=>$data['keyword'],'info_type'=>'multi');
            Model_Table::get('KeywordList')->saveForKeywords($arr);
            
        }else{//非第一条图文
            if($data['child_id']){//修改
                Model_Table::get('InfoCommonDetail')->updateById($data['child_id'],array(
                    'title'=>$data['title'],'pic'=>$data['pic'],
                    'msg_content'=>$data['msg_content'],'url'=>$data['url'],
                    'pic_showincontent'=>$data['pic_showincontent'],'order_num'=>$data['order_num']
                ));
            }else{//保存
                Model_Table::get('InfoCommonDetail')->insert(array(
                    'info_common_id'=>$data['id'],'title'=>$data['title'],'pic'=>$data['pic'],
                    'msg_content'=>$data['msg_content'],'url'=>$data['url'],
                    'pic_showincontent'=>$data['pic_showincontent'],'order_num'=>$data['order_num']
                ));
            }
        }
        die('suc');
	}else{
		$id=(int)$_GET['id'];
		if($id){
			$smarty->assign('infoRow',$model->find($id));
            //详情
            $infoAll=D('info_common_detail')->where("info_common_id='{$id}'")->order("order_num asc,id asc")->dataArr();
            foreach($infoAll as $k=>$v){
                $infoAll[$k]['short_title']=cut_str(deletehtml($v['title']),8);
            }
			$smarty->assign('infoAll',$infoAll);
		}
		$smarty->setTpl('info/templates/multi_edit.html')->display();die();
	}
}

//数据删除
if($_REQUEST['a']=='del'){
    $res=$model->del((int)$_POST['id']);
    $model->query("delete from keyword_list where info_id='".(int)$_POST['id']."' and customer_id='{$customer_id}'");
    die('suc');
}

//数据列表
$filter=array();
$filter['where'] = "customer_id='{$customer_id}' and info_type='3' ";
if($_GET['keyword']) $filter['where'].=" and keyword like '%".common_pg('keyword')."%'";
$filter['order'] = "id desc";
$data = $model->paginate($filter,'*',common_pg('p'),10);
$listArr = $data['data'];
$infoDetailModel=D('info_common_detail');
foreach($listArr as $k=>$v){
    $dRow=$infoDetailModel->field("count(*) as count_num")->where("info_common_id='".$v['id']."'")->dataRow();
    $listArr[$k]['item_num']=$dRow['count_num'];
}

$smarty->assign('list',$listArr);
$smarty->assign('page',$model->pager($data['pager']));

//敏感词
$row=array();
$arr = D('wx_sense')->dataArr();
foreach($arr as $k=>$v){
    if($v['word']) $row[]=$v['word'];
}
$senseRow = $row ? json_encode($row) : '';
$smarty->assign('senseRow',$senseRow);

$smarty->setTpl('info/templates/multi_index.html')->display();